import Editor from "./../editor";
import {
    INPUT_TYPE,
} from '../constants/inputType'
import isHotkey from "is-hotkey"
import type { Selection } from "../models"
/** 键盘事件 */
export class KeyboardEvent {
    private _editor :Editor;
    private _container : HTMLElement;
    /**中文选区 */
    private _compositionSelection : Selection = {
        anchor: 0,
        focus: 0,
    }
    /**是否中文输入中 */
    private _compositionFlag = false;

    constructor (editor:Editor, container:HTMLElement) {
        this._editor = editor;
        this._container = container;
    }

    _beforeInputHandler (e: InputEvent) {
        const inputType = e.inputType;
        /** 文本 */
        const text = e.data;
        /** 快键键文本 */
        const textPaste = e.dataTransfer
        e.preventDefault();
        /** 中文输入默认忽略，走输入法监听逻辑 */
        if(inputType === 'insertCompositionText' || inputType === 'deleteCompositionText'){
            return;
        }
        switch (inputType) {
            /**插入 */
            case INPUT_TYPE.INSERT: 
                if (text) {
                    this._editor.insertTextAtCursor(text);
                }
                break;
            /**回车 */
            case INPUT_TYPE.ENTER:
                this._editor.insertTextAtCursor('\n');
                break;
            /**删除 */
            case INPUT_TYPE.REMOVE:
                this._editor.removeTextAtCursor('')
                break;
            /**粘贴插入文本 */
            case INPUT_TYPE.FROMPASTE:
                // 后续如果想要对其他内容处理也可以扩展，比如图片
                if (textPaste?.getData('text/plain')) {
                  this._editor.insertTextAtCursor(textPaste?.getData('text/plain'));
                }
                break
        }
    }
    /** globalThis作为顶层对象 */
    _keydownHandler (e:globalThis.KeyboardEvent) {
        /**撤销 */
        if (isHotkey("Control+z")(e)) {        
            this._editor.undo && this._editor.undo()
        }
        /**回退 */
        if (isHotkey("Control+Y")(e)) {
            this._editor.redo && this._editor.redo()
        }
    }

    /** 输入法结束时 */
    _compositionendHandler(e: CompositionEvent){
        this._compositionFlag = true;
        /**中文输入期间阻止默认事件，也不要进入editor的逻辑。等输入结束后再放行，这样中间状态就不会污染到数据模型 */
        e.preventDefault()
        e.data ? this._editor.insertCompositionAtCursor(e.data,this._compositionSelection) : null
    }

    /** 输入法开始时 */
    _compositionstartHandler(){
       this._compositionFlag = false;
       /**中文污染英文选区，因此从输入法开始获取选区 */
       this._compositionSelection = Object.assign( {}, this._editor.getSelectionModel().getSelection())
  
      if (this._compositionSelection.anchor > this._compositionSelection.focus) {
        [this._compositionSelection.anchor,this._compositionSelection.focus] =
            [this._compositionSelection.focus,this._compositionSelection.anchor]
      }
    }   

    /** 事件监听 */
    addListeners() {
        this._container.addEventListener('beforeinput', this._beforeInputHandler.bind(this))
        this._container.addEventListener('keydown', this._keydownHandler.bind(this))
        this._container.addEventListener('compositionend', this._compositionendHandler.bind(this))
        this._container.addEventListener("compositionstart",this._compositionstartHandler.bind(this))
    }
    /** 事件撤销 */
    removeListeners(){
        this._container.removeEventListener('beforeinput',this._beforeInputHandler)
        this._container.removeEventListener('keydown',this._keydownHandler)
        this._container.removeEventListener('compositionend', this._compositionendHandler)
        this._container.removeEventListener("compositionstart",this._compositionstartHandler)
    }
}